﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.RenderTemplete.Controls;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service; 

namespace Exhibition.Manage
{
    public partial class ContentDetail : BaseAdminPage
    {
        ContentModel model = new ContentModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hfId.Value = reqID.ToString();
                BindDrop();
                GetContent();
            }
        }

        private void BindDrop()
        {
            new NavTreeList().DropDownTree.Bind(drpList, "", "0");
            drpList.Items.Insert(0, new ListItem("请选择栏目", "0"));
            List<CodesModel> mdls = CenterService.codesService.GetModels(1);
            rdoList.DataTextField = "EName";
            rdoList.DataValueField = "ECode";
            rdoList.DataSource = mdls;
            rdoList.DataBind();
        }

        private void GetContent()
        {
            model = CenterService.contentService.GetOneById(reqID);
            if (model != null)
            {

                txtCreatUserName.Value = model.CreatUserName == "" ? AdminUser.RealName : model.CreatUserName;

                //中文标题
                txtTitle.Value = model.Title.ToString();

                //英文标题
                 txtETitle.Value = model.ETitle.ToString();

                //标题颜色
                TitleColor.Value = model.TitleColor.ToString();

                //属性
                if (!string.IsNullOrEmpty(model.Flags))
                {
                    var arry = model.Flags.Split(',');
                    for (int i = 0; i < arry.Length; i++)
                    {
                        for (int j = 0; j < rdoList.Items.Count; j++)
                        {
                            if (rdoList.Items[j].Value == arry[i])
                            {
                                rdoList.Items[j].Selected = true;
                            }
                        }
                    }
                }

                //权重
                txtWeight.Value = model.Weight.ToString();


                //图片
                hfPic.Value = model.PicPath;
                spPic.Src = string.Format("{0}", model.PicPath == "" ? "../images/nopic.jpg" : model.PicPath);
                //来源
                txtIncome.Value = model.Income;

                //栏目ID 
                drpList.SelectedValue = model.NavId.ToString();
                //关键字
                txtKeyWord.Value = model.KeyWord;

                //英文关键字
                txtEKeyWord.Value = model.EKeyWord;

                //摘要
                txtDesc.Text = model.Description;

                //英文摘要
                txtEDesc.Text = model.EDesc;

                //发布时间
                //txtCreateTime.Value = model.CreateTime.ToString();

                //中文内容
                txtContent.Text = model.Content;

                //英文内容
                txtEContent.Text = model.EContent;

                //发布人
                //txtCreatUserId.Value = model.CreatUserId.ToString();

                //发布人名
                txtCreatUserName.Value = model.CreatUserName;

                //更新时间
                //txtUpdateTime.Value = model.UpdateTime.ToString();

                //点击量
                //txtClick.Value = model.Click.ToString();

                //SiteID
                //txtSiteID.Value = model.SiteID.ToString();

            }
            else {
                txtWeight.Value = "0";
            }
        }

        /// <summary>
        /// 保存一条数据
        /// <summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            model = string.IsNullOrEmpty(hfId.Value) ||hfId.Value=="0"? new ContentModel() : CenterService.contentService.GetOneById(Convert.ToInt32(hfId.Value));
            //中文标题
            model.Title = txtTitle.Value;

            //英文标题
            //model.ETitle = txtETitle.Value;

            //标题颜色
            model.TitleColor = TitleColor.Value;

            //属性
            var stFlg = "";
            for (int i = 0; i < rdoList.Items.Count; i++)
            {
                if (rdoList.Items[i].Selected) {
                    stFlg +=rdoList.Items[i].Value+ ",";
                }
            }
            model.Flags = stFlg;

            //权重
            model.Weight = Convert.ToInt32(txtWeight.Value);

            //图片
            model.PicPath = hfPic.Value;

            //来源
            model.Income = txtIncome.Value;

            //栏目ID
            model.NavId = Convert.ToInt32(drpList.SelectedValue);

            //关键字
            model.KeyWord = txtKeyWord.Value;

            //英文关键字
            //model.EKeyWord = txtEKeyWord.Value;

            //摘要
            model.Description = txtDesc.Text;

            //英文摘要
            //model.EDesc = txtEDesc.Value;
            if (hfId.Value == "0")
            {
                //发布时间
                model.CreateTime = DateTime.Now;
                //发布人
                model.CreatUserId = AdminUser.Id;
                //发布人名
                model.CreatUserName = txtCreatUserName.Value;
            }
            else
            {
                model.UpdateTime = DateTime.Now;
            }
            //中文内容
            model.Content = txtContent.Text;

            //英文内容
            //model.EContent = txtEContent.Value;

            model.SiteID = CurrentSite.Id;

         

            //更新时间
            //model.UpdateTime = Convert.ToDateTime(txtUpdateTime.Value);

            //点击量
            //model.Click = Convert.ToInt32(txtClick.Value);

            //SiteID
           // model.SiteID = Convert.ToInt32(txtSiteID.Value);

            if (CenterService.contentService.Save(model) > 0)
            {

                JsHelper.Unblock(this, CommFunMsg.GetMessage("a0001", Lan.zh));
                return;
            }
            else {
                JsHelper.Alert(this, CommFunMsg.GetMessage("e0001", Lan.zh));
                return;
            }

        }
    }
}